<template>
  <div class="container mtop">
    <m-header title="邀请好友"></m-header>
    <div class="contents">
      <div class="android" v-if="isAndroid">
        <img class="codeimg" :src="inviteData.android_qr" alt="">
        <!-- <div class="inviteQr">Android邀请二维码</div> -->
      </div>
      <div class="ios" v-else>
        <img class="codeimg" :src="inviteData.ios_qr" alt="">
        <!-- <div class="inviteQr">IOS邀请二维码</div> -->
      </div>
      <!-- <div class="types">
        <span class="text">邀 请 码：</span>
        <span class="val">{{inviteData.no}}</span>
      </div> -->
      <div class="types">
        <span class="text">推广链接：</span>
        <span class="val">{{shareUrl}}</span>
        <span class="copytxt"
          v-clipboard:copy="shareUrl"
          v-clipboard:success="onCopy"
          v-clipboard:error="onError"
        >复制</span>
      </div>
    </div>
  </div>
</template>
<script>
import { mapGetters, mapActions } from 'vuex';

export default {
  components: {},
  data () {
    return {
      shareUrl: '',
      inviteData: {
        // no: '000005',
        // url: 'http://baidu.com',
        // qr: ''
      }
    };
  },
  computed: {
    ...mapGetters({
      userInfo: 'mine/userInfo'
    }),
    isAndroid () {
      const u = navigator.userAgent;
      return u.indexOf('Android') > -1 || u.indexOf('Linux') > -1;
    }
  },
  mounted () {
    // if (this.userInfo.member_id) {
    //   this.getInvitation();
    // } else {
    //   this.getMineInfo();
    // }
    this.getInvitation();
  },
  methods: {
    ...mapActions({
      getMineInfo: 'mine/mineInfo',
      getInvitationCode: 'mine/getInvitationCode'
    }),
    getInvitation () {
      const params = {
        // member_id: '1', // 会员id
        // system
      }
      this.getInvitationCode(params).then(res => {
        if (res) {
          this.inviteData = res;
          if (this.isAndroid) {
            this.shareUrl = res.android_url;
          } else {
            this.shareUrl = res.ios_url;
          }
        }
      });
    },
    onCopy: function (e) {
      console.log(e.text);
      this.$vux.confirm.show({
      // title: '提示',
        content: '复制成功',
        showCancelButton: false,
        onCancel () {
          console.log(this) // 非当前 vm
          console.log(_this) // 当前 vm
        },
        onConfirm () {}
      })
    },
    onError: function (e) {
      // alert('Failed to copy texts')；
      this.$vux.toast.text('复制失败，请稍后重试！');
    }
  }
};
</script>
<style lang="less" scoped>
  @import '../../../assets/styles/mixins.less';
.container {
  // background: #f7f7f7;
  .contents {
    padding-top: 0.7rem;
    margin: 0 0.5rem;
    .codeimg {
      display: block;
      width: 3.12rem;
      height: 3.12rem;
      padding: 0.2rem;
      margin: 0 auto;
      background: #FFFFFF;
      border: 0.01rem solid #979797;
      box-sizing: border-box;
      // background: #D44743;
    }
    .inviteQr {
      line-height: 0.8rem;
      font-size: 0.28rem;
      text-align: center;
    }
    .types {
      position: relative;
      width: 100%;
      display: inline-flex;
      align-items: center;
      margin:  0.45rem auto;
      // margin:  0.4rem 0;
      // margin:  0.2rem 0;
      padding: 0 1.17rem 0 0.5rem;
      height: 0.8rem;
      // line-height: 0.8rem;
      background: #FFFFFF;
      border: 1px solid #d2d2d2;
      border-radius: 0.4rem;
      box-sizing: border-box;
      overflow: hidden;
      // .border-1px(#d2d2d2);
      .text {
        display: inline-block;
        width: 1.4rem;
        height: 100%;
        line-height: 0.8rem;
      }
      .val {
        flex: 1;
        display: inline-block;
        overflow: hidden;
        // white-space: nowrap;
        // text-overflow: ellipsis;
      }
      .copytxt {
        position: absolute;
        right: 0;
        top: 0;
        display: flex;
        align-items: center;
        width: 1.17rem;
        height: 100%;
        padding-left: 0.24rem;
        font-size: 0.26rem;
        color: #D44743;
        box-sizing: border-box;
      }
    }
  }
}
</style>
